<?php
/*
	预约订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$store_id=$this->session('store_id');
if(!$store_id){
	exi('请先选择场馆！','error');
}
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$tables = pdo_getall('jy_qiuyuyue_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('title','id'));
	foreach($tables as &$t){
			$t['table_list']=pdo_getall('jy_qiuyuyue_table',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'status'=>1,'group_id'=>$t['id']));
	}
	$yuding = pdo_getall('jy_qiuyuyue_order_reservation',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'mid'=>$_SESSION['uid'],'status <'=>2));
	$yuding =$yuding[0];
	if($yuding){
		$return['isyuding']=1;
		if($yuding['table_id']){
			$table = pdo_get('jy_qiuyuyue_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$yuding['table_id']),array('title','id'));
			$yuding['tablename']=$table['title'].'('.$tables[$yuding['group_id']]['title'].')';
		}else{
			$yuding['tablename']='现场入座('.$tables[$yuding['group_id']]['title'].')';
		}
	}
	if($yuding['status'] == 0){
		$yuding['status'] = '待处理';
	}else{
		$yuding['status'] = '商家已接单';
	}
	$return['tables']=$tables;
	$return['yuding']=$yuding;
	set_html('title','预约订餐');
	$return['post']=array();
	exi($return);
}
	//提交订单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$store_id=$this->session('store_id');
	if(!$store_id){
		exi('请先选择场馆！','error','jy_qiuyuyue/store_list/');
	}
	$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'store_id'=>$store_id,
				'table_id'=>intval($post['table_id']),
				'group_id'=>intval($post['group_id']),
				'user_num'=>intval($post['user_num']),
				'user_time'=>trim($post['user_time']),
				'user_name'=>trim($post['user_name']),
				'mobile'=>trim($post['mobile']),
				'status'=>0,
				'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_qiuyuyue_order_reservation',$data);
	$nickname=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'nickname');
	$tpldata=array(
		'first'=>array('value'=>'预约订餐通知','color'=>''),
		'tradeDateTime'=>array('value'=>'预约时间:'.$post['user_time'],'color'=>'#ff510'),
		'orderType'=>array('value'=>'用餐人数:'.$post['user_num'],'color'=>'#ff510'),
		'customerInfo'=>array('value'=>'联系人:'.$post['user_name'],'color'=>'#ff510'),
		'remark'=>array('value'=>'联系电话:'.$post['mobile'],'color'=>'#ff510'),
	);
	$url=custom_url('jy_qiuyuyue/reservation',true);
	$text = '预约订餐通知/n';
	$text .='预约时间:'.$post['user_time'].'/n';
	$text .='用餐人数:'.$post['user_num'].'/n';
	$text .='联系人:'.$post['user_name'].'/n';
	$text .='联系电话:'.$post['mobile'].'/n';
	//发信息
	$this->notice('yuding_new',$text,$tpldata,$_SESSION['uid'],$url,'#980000');
	//打印
	$this->printmsg($text,5,$store_id);
	exi('预订成功！');
}